#!/usr/bin/env sh

# Delete all existing indices.
curl -XDELETE 'http://localhost:9200/_all/'

# Create the PCAP index.
curl -XPUT 'http://localhost:9200/pcap_all/' -d '{
  "settings": {
      "index.codec.bloom.load": false,
      "index.compound_on_flush": false,
      "index.compound_format": false,
      "index.merge.policy.use_compound_file": false,
      "index.refresh_interval": 120,
      "index.action.write_consistency": false,
      "index.translog.flush_threshold_ops": 100000,
      "index.compound_on_flush": false,
      "index.compound_format": false,
      "index.merge.policy.use_compound_file": false
  },
  "mappings": {
    "pcap_doc": {
      "_all": {
        "enabled": false
      },
      "_source": {
        "includes": ["message.ts_micro", "message.pcap_id", "message.ip_id", "message.frag_offset"]
      },
      "properties": {
        "message" : {
          "properties":{
            "pcap_id": {
              "type": "string",
              "index": "not_analyzed",
              "postings_format": "Lucene41"
            },
            "ip_protocol": {
                "type": "long",
                "precision_step": 64
            },
            "ip_src_addr": {
                "type": "string",
                "index": "not_analyzed",
                "postings_format": "Lucene41"
            },
            "ip_src_port": {
                "type": "long",
                "precision_step": 64
            },
            "ip_dst_addr": {
                "type": "string",
                "index": "not_analyzed",
                "postings_format": "Lucene41"
            },
            "ip_dst_port": {
                "type": "long",
                "precision_step": 64
            },
            "ts_micro": {
                "type": "long",
                "precision_step": 64
            },
            "ip_id": {
                "type": "long",
                "precision_step": 64,
                "index": "no"
            },
            "frag_offset": {
                "type": "long",
                "precision_step": 64,
                "index": "no"
            }
          }
        }
      }
    }
  }
}'

# Seed Elasticsearch.
for file in seed/es/*.json
do
  curl -s -XPOST --data-binary @$file 'http://localhost:9200/_bulk'
done

echo "\nDone."
